<?php
/**
 * @version 	$Id: fLoader.class.php 74 2010-03-17 06:48:21Z mylifeisskidrow@gmail.com $
 * @author 		$Author: mylifeisskidrow@gmail.com $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */
defined('IN_FLYWHEEL') or die('Restricted access!');

/**
 * Loader
 * 	Class import cac thanh pha cua framework
 * 	
 * @package 	system
 */

class fLoader {
	/**
	 * import
	 *
	 * @param string $file
	 * @param string $package
	 * @param string $extension
	 * @param boolean $return
	 * @return boolean, false if file not exist
	 */
	public static function import($file, $package = 'systems', $extension = 'class.php', $return = false) {		
		if ($package == '') $package = 'systems';
		switch ($package) {
			case 'vendors':
				$path = (defined('_VENDORS_PATH_'))? _VENDORS_PATH_ : _ROOT_PATH_.'vendors'._DS_;
				break;
			default:
				$path = _SYSTEMS_PATH_;
				break;
		}		
		$path = substr($path, 0, strrpos($path, _DS_)) ._DS_;
		$file = str_replace('.', _DS_, $file);		
		if (!file_exists($filePath = $path.$file.'.'.$extension)) {
			return false;
		}			
		else {
			if ($return) return require_once $filePath;
			else require_once $filePath;
			return true;					
		}
	}
	
	/**
	 * Load Vendor
	 *
	 * @param string $name		Vendor name
	 * @param string $file		File Path
	 * @param string $extension File extension default = '.php'
	 * @return void
	 */
	public static function loadVendor($name, $file, $extension = 'php') {
		self::import($name.'.'.$file, 'vendors', $extension);				
//		$path = _ROOT_PATH_.'vendors'._DS_.$name;
//		$path = rtrim($path, _DS_);
//		$file = str_replace('.', _DS_, $file);
//		if (!file_exists($filePath = $path._DS_.$file.$extension)) {
//			echo $filePath;			
//			return false;
//		}
//		
//		require_once $filePath;		
	}
	
	/**
	 * Load Model
	 *
	 * @param string $name		Model name
	 * @param string $package	Model package
	 */
	public static function loadModel($name, $package = null) {
		if ($package != null) {
			$package = str_replace('.', _DS_, $package)._DS_;
			$package = ltrim($package, _DS_);				
		}
		$name = ucfirst($name).'Model';
		if (!file_exists($filePath = _ROOT_PATH_.'models'._DS_.$package.$name.'.php')) {
			//throw Exception
			echo $filePath;
			return false;
		}
		
		require_once $filePath;
		
	}
}